﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;

namespace CrmApplication.Models
{
    public class SubdomainModels
	{
		#region RegionMappindSubdomain
		
		public decimal Id{ get; set; }
        public string Name { get; set; }
        public decimal IdDomain { get; set; }

		#endregion

		#region RegionOracleConnect

		private static OracleConnection connect;

        static SubdomainModels()
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["HomeOrcl"].ConnectionString;
            connect = new OracleConnection(connString);
        }

		#endregion

		#region RegionSubdomain
		/// <summary>
        /// get subdomains list
        /// </summary>
        /// <param name="searchDomain"></param>
        /// <param name="domainId"></param>
        /// <returns></returns>
        public static List<SubdomainModels> GetSubdomain(string searchDomain,decimal domainId)
        {
            string selectDomain = "and subdomain_name like '" + searchDomain + "%'";
            string selectString = "SELECT id_subdomain,subdomain_name,id_domain from subdomain where id_domain =:domainId "
                                    + selectDomain + " order by subdomain_name ";

            OracleCommand loginComm = new OracleCommand(selectString);

            loginComm.Parameters.Add(":domainId", domainId);

            loginComm.Connection = connect;

            List<SubdomainModels> result = new List<SubdomainModels>();
            SubdomainModels result1 = null;
            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result1 = new SubdomainModels();
                        result1.Id = sdr.GetDecimal(0);
                        result1.Name = sdr.GetString(1);
                        result1.IdDomain = sdr.GetDecimal(2);
                        result.Add(result1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }
        
        /// <summary>
        /// add new subdomain
        /// </summary>
        /// <param name="subdomainName"></param>
        /// <param name="domainId"></param>
        public static void AddSubdomain(string subdomainName, decimal domainId)
        {
            string insert = "INSERT INTO subdomain(subdomain_name,id_domain) VALUES (:subdomainName,:domainId)";

            OracleCommand usercomm = new OracleCommand(insert);

            usercomm.Parameters.Add(":subdomainName", subdomainName);
            usercomm.Parameters.Add(":domainId", domainId);

            usercomm.Connection = connect;
            try
            {
                connect.Open();
                usercomm.ExecuteNonQuery();
            }
            finally
            {
                connect.Close();
            }
        }

        /// <summary>
        /// creat subdomain array for json
        /// </summary>
        /// <param name="searchDomain"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public static SubdomainContainer GetSubdomainArray(string searchDomain, decimal parentId)
        {
            SubdomainModels[] data = new SubdomainModels[20];
            data = SubdomainModels.GetSubdomain(searchDomain,parentId).ToArray();

            SubdomainContainer container = new SubdomainContainer()
            {
                Records = data,
                Count = data.Length
            };

            return container;
		}
		#endregion
	}

    public class SubdomainContainer
    {
        public SubdomainModels[] Records { get; set; }
        public int Count { get; set; }
    }
    }
